﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

[Serializable()]
public enum PLAYER_STATUS
{
    DEAD,
    ALIVE,
    HAS_FLAG,
    HAS_FLAG_RETURNED,
    IN_SPAWN_POINT
};

namespace BL
{
    [Serializable()]
    public class cPlayer
    {
        public delegate void PlayerUpdateListener(cPlayer pl);
        public event PlayerUpdateListener LocationUpdate;
        public event PlayerUpdateListener PlayerHasFlag;
        public event PlayerUpdateListener PlayerIsDead;
        public event PlayerUpdateListener PlayerReturnedFlag;
        public event PlayerUpdateListener PlayerInSpawnPoint;
        public event PlayerUpdateListener PlayerRevived;

        public cPlayer(int playerId, string name, string picture, int startPort)
        {
            ID = playerId;
            Name = name;
            Picture = picture;
            Status = PLAYER_STATUS.ALIVE;
            LocationManger = new cLocationManager(startPort, ID);
            LocationManger.LocationUpdate += new cLocationManager.LocationUpdateLisner(OnLocationUpdated);
            LocationManger.DataReceived += LocationManger_DataReceived;
        }

        void LocationManger_DataReceived(PLAYER_STATUS status)
        {
            if (Status != status)
                Status = status;
        }

        PLAYER_STATUS m_status;
        public PLAYER_STATUS Status
        {
            get { return m_status; }
            set 
            {
                if (m_status != value)
                {
                    switch (value)
                    {
                        case PLAYER_STATUS.ALIVE:
                            if (PlayerRevived != null) 
                            {
                                // If player was previously dead
                                if (m_status.Equals(PLAYER_STATUS.DEAD))
                                {
                                    PlayerRevived(this);
                                }
                            }
                            break;
                        case PLAYER_STATUS.DEAD:
                            if (PlayerIsDead != null)
                                PlayerIsDead(this);
                            break;
                        case PLAYER_STATUS.HAS_FLAG:
                            if (PlayerHasFlag != null)
                                PlayerHasFlag(this);
                            break;
                        case PLAYER_STATUS.HAS_FLAG_RETURNED:
                            if (PlayerReturnedFlag != null)
                                PlayerReturnedFlag(this);
                            break;
                        case PLAYER_STATUS.IN_SPAWN_POINT:
                            if (PlayerInSpawnPoint != null)
                                PlayerInSpawnPoint(this);
                            break;
                        default:
                            break;
                    }

                    m_status = value;
                }
            }
        }

        int n_nid;
        public int ID
        {
            get { return n_nid; }
            set { n_nid = value; }
        }
        string m_strName;

        public string Name
        {
            get { return m_strName; }
            set { m_strName = value; }
        }

       string m_strPic;

        public string Picture
        {
            get { return m_strPic; }
            set { m_strPic = value; }
        }

        cLocationManager m_LocationManger;

        public cLocationManager LocationManger
        {
            get { return m_LocationManger; }
            set { m_LocationManger = value; }
        }

        public void OnLocationUpdated(Point p)
        {
            if (LocationUpdate != null)
                LocationUpdate(this);
        }

        public Point GetCurrLocation()
        {
            return LocationManger.CurrLocation;
        }

        public Point GetPreviousLocation()
        {
            return LocationManger.PrevLocation;
        }

        public void RefreshConnection(int startPort)
        {
            LocationManger.RefreshConnection(startPort, ID);
        }
    }
}
